{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "train=pd.read_csv('F:\\\\CSDN\\\\pima-indians-diabetes.csv')"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "特征变换：\n",
    "1.取对数log1p(对线性模型很重要，单调变换树模型影响不大)\n",
    "2.tf-idf\n",
    "3.原始特征组合（加减乘除，如果是计数特征，乘法表示‘and',或者可以采用GBDT做特征编码，实现更高阶特征组合；原始特征位数太高，可以先用特征模型得到特征的重要性，对重要的特征在组合）\n",
    "4.t-SNE及PCA降维后的特征\n",
    "5.统计特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#分开特征和标签\n",
    "y_train=train['Target']\n",
    "\n",
    "X_train=train.drop(['Target'],axis=1)\n",
    "\n",
    "#保存特征的名字 \n",
    "columns_org=X_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants</th>\n",
       "      <th>Plasma_glucose_concentration</th>\n",
       "      <th>blood_pressure</th>\n",
       "      <th>Triceps_skin_fold_thickness</th>\n",
       "      <th>serum_insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>Diabetes_pedigree_function</th>\n",
       "      <th>Age</th>\n",
       "      <th>Target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6</td>\n",
       "      <td>148</td>\n",
       "      <td>72</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>33.6</td>\n",
       "      <td>0.627</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>26.6</td>\n",
       "      <td>0.351</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>183</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.3</td>\n",
       "      <td>0.672</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>89</td>\n",
       "      <td>66</td>\n",
       "      <td>23</td>\n",
       "      <td>94</td>\n",
       "      <td>28.1</td>\n",
       "      <td>0.167</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>137</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>168</td>\n",
       "      <td>43.1</td>\n",
       "      <td>2.288</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pregnants  Plasma_glucose_concentration  blood_pressure  \\\n",
       "0          6                           148              72   \n",
       "1          1                            85              66   \n",
       "2          8                           183              64   \n",
       "3          1                            89              66   \n",
       "4          0                           137              40   \n",
       "\n",
       "   Triceps_skin_fold_thickness  serum_insulin   BMI  \\\n",
       "0                           35              0  33.6   \n",
       "1                           29              0  26.6   \n",
       "2                            0              0  23.3   \n",
       "3                           23             94  28.1   \n",
       "4                           35            168  43.1   \n",
       "\n",
       "   Diabetes_pedigree_function  Age  Target  \n",
       "0                       0.627   50       1  \n",
       "1                       0.351   31       0  \n",
       "2                       0.672   32       1  \n",
       "3                       0.167   21       0  \n",
       "4                       2.288   33       1  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. feat编码：log(x+1)\n",
    "原始特征feat_x看起来像计数特征，取log运算更接近人对数字的敏感度，更适合线性模型。 同时也可以降低长维分布中大数值的影响，减弱长维分布的长尾性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants</th>\n",
       "      <th>Plasma_glucose_concentration</th>\n",
       "      <th>blood_pressure</th>\n",
       "      <th>Triceps_skin_fold_thickness</th>\n",
       "      <th>serum_insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>Diabetes_pedigree_function</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.945910</td>\n",
       "      <td>5.003946</td>\n",
       "      <td>4.290459</td>\n",
       "      <td>3.583519</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.543854</td>\n",
       "      <td>0.486738</td>\n",
       "      <td>3.931826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.693147</td>\n",
       "      <td>4.454347</td>\n",
       "      <td>4.204693</td>\n",
       "      <td>3.401197</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.317816</td>\n",
       "      <td>0.300845</td>\n",
       "      <td>3.465736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.197225</td>\n",
       "      <td>5.214936</td>\n",
       "      <td>4.174387</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.190476</td>\n",
       "      <td>0.514021</td>\n",
       "      <td>3.496508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.693147</td>\n",
       "      <td>4.499810</td>\n",
       "      <td>4.204693</td>\n",
       "      <td>3.178054</td>\n",
       "      <td>4.553877</td>\n",
       "      <td>3.370738</td>\n",
       "      <td>0.154436</td>\n",
       "      <td>3.091042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.927254</td>\n",
       "      <td>3.713572</td>\n",
       "      <td>3.583519</td>\n",
       "      <td>5.129899</td>\n",
       "      <td>3.786460</td>\n",
       "      <td>1.190279</td>\n",
       "      <td>3.526361</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pregnants  Plasma_glucose_concentration  blood_pressure  \\\n",
       "0   1.945910                      5.003946        4.290459   \n",
       "1   0.693147                      4.454347        4.204693   \n",
       "2   2.197225                      5.214936        4.174387   \n",
       "3   0.693147                      4.499810        4.204693   \n",
       "4   0.000000                      4.927254        3.713572   \n",
       "\n",
       "   Triceps_skin_fold_thickness  serum_insulin       BMI  \\\n",
       "0                     3.583519       0.000000  3.543854   \n",
       "1                     3.401197       0.000000  3.317816   \n",
       "2                     0.000000       0.000000  3.190476   \n",
       "3                     3.178054       4.553877  3.370738   \n",
       "4                     3.583519       5.129899  3.786460   \n",
       "\n",
       "   Diabetes_pedigree_function       Age  \n",
       "0                    0.486738  3.931826  \n",
       "1                    0.300845  3.465736  \n",
       "2                    0.514021  3.496508  \n",
       "3                    0.154436  3.091042  \n",
       "4                    1.190279  3.526361  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log=np.log1p(X_train)\n",
    "log.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants_log</th>\n",
       "      <th>Plasma_glucose_concentration_log</th>\n",
       "      <th>blood_pressure_log</th>\n",
       "      <th>Triceps_skin_fold_thickness_log</th>\n",
       "      <th>serum_insulin_log</th>\n",
       "      <th>BMI_log</th>\n",
       "      <th>Diabetes_pedigree_function_log</th>\n",
       "      <th>Age_log</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.945910</td>\n",
       "      <td>5.003946</td>\n",
       "      <td>4.290459</td>\n",
       "      <td>3.583519</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.543854</td>\n",
       "      <td>0.486738</td>\n",
       "      <td>3.931826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.693147</td>\n",
       "      <td>4.454347</td>\n",
       "      <td>4.204693</td>\n",
       "      <td>3.401197</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.317816</td>\n",
       "      <td>0.300845</td>\n",
       "      <td>3.465736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.197225</td>\n",
       "      <td>5.214936</td>\n",
       "      <td>4.174387</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.190476</td>\n",
       "      <td>0.514021</td>\n",
       "      <td>3.496508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.693147</td>\n",
       "      <td>4.499810</td>\n",
       "      <td>4.204693</td>\n",
       "      <td>3.178054</td>\n",
       "      <td>4.553877</td>\n",
       "      <td>3.370738</td>\n",
       "      <td>0.154436</td>\n",
       "      <td>3.091042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.927254</td>\n",
       "      <td>3.713572</td>\n",
       "      <td>3.583519</td>\n",
       "      <td>5.129899</td>\n",
       "      <td>3.786460</td>\n",
       "      <td>1.190279</td>\n",
       "      <td>3.526361</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pregnants_log  Plasma_glucose_concentration_log  blood_pressure_log  \\\n",
       "0       1.945910                          5.003946            4.290459   \n",
       "1       0.693147                          4.454347            4.204693   \n",
       "2       2.197225                          5.214936            4.174387   \n",
       "3       0.693147                          4.499810            4.204693   \n",
       "4       0.000000                          4.927254            3.713572   \n",
       "\n",
       "   Triceps_skin_fold_thickness_log  serum_insulin_log   BMI_log  \\\n",
       "0                         3.583519           0.000000  3.543854   \n",
       "1                         3.401197           0.000000  3.317816   \n",
       "2                         0.000000           0.000000  3.190476   \n",
       "3                         3.178054           4.553877  3.370738   \n",
       "4                         3.583519           5.129899  3.786460   \n",
       "\n",
       "   Diabetes_pedigree_function_log   Age_log  \n",
       "0                        0.486738  3.931826  \n",
       "1                        0.300845  3.465736  \n",
       "2                        0.514021  3.496508  \n",
       "3                        0.154436  3.091042  \n",
       "4                        1.190279  3.526361  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat_names=X_train.columns+'_log'\n",
    "X_train_log=pd.DataFrame(columns=feat_names,data=log.values)\n",
    "X_train_log.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. feat编码：TF-IDF\n",
    "原始特征feat_x看起来像计数特征，类似文本分析中词频特征的处理，TF-IDF可以突出对特别类别有贡献的低频词。 这里原始特征已经是计数特征了，直接调用TfidfTransformer，将计数特征变成TF-IDF 如果输入是原始文本，需要将计数功能（TF）和IDF功能集中在一起，用TfidfVectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants_tfidf</th>\n",
       "      <th>Plasma_glucose_concentration_tfidf</th>\n",
       "      <th>blood_pressure_tfidf</th>\n",
       "      <th>Triceps_skin_fold_thickness_tfidf</th>\n",
       "      <th>serum_insulin_tfidf</th>\n",
       "      <th>BMI_tfidf</th>\n",
       "      <th>Diabetes_pedigree_function_tfidf</th>\n",
       "      <th>Age_tfidf</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.037717</td>\n",
       "      <td>0.810132</td>\n",
       "      <td>0.409804</td>\n",
       "      <td>0.256931</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.185363</td>\n",
       "      <td>0.003410</td>\n",
       "      <td>0.271919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.009341</td>\n",
       "      <td>0.691357</td>\n",
       "      <td>0.558183</td>\n",
       "      <td>0.316326</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.218049</td>\n",
       "      <td>0.002836</td>\n",
       "      <td>0.250508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.046188</td>\n",
       "      <td>0.920021</td>\n",
       "      <td>0.334562</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.118057</td>\n",
       "      <td>0.003357</td>\n",
       "      <td>0.159835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.005813</td>\n",
       "      <td>0.450469</td>\n",
       "      <td>0.347351</td>\n",
       "      <td>0.156119</td>\n",
       "      <td>0.787603</td>\n",
       "      <td>0.143341</td>\n",
       "      <td>0.000840</td>\n",
       "      <td>0.105602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.426849</td>\n",
       "      <td>0.129587</td>\n",
       "      <td>0.146243</td>\n",
       "      <td>0.866498</td>\n",
       "      <td>0.135338</td>\n",
       "      <td>0.007082</td>\n",
       "      <td>0.102151</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pregnants_tfidf  Plasma_glucose_concentration_tfidf  blood_pressure_tfidf  \\\n",
       "0         0.037717                            0.810132              0.409804   \n",
       "1         0.009341                            0.691357              0.558183   \n",
       "2         0.046188                            0.920021              0.334562   \n",
       "3         0.005813                            0.450469              0.347351   \n",
       "4         0.000000                            0.426849              0.129587   \n",
       "\n",
       "   Triceps_skin_fold_thickness_tfidf  serum_insulin_tfidf  BMI_tfidf  \\\n",
       "0                           0.256931             0.000000   0.185363   \n",
       "1                           0.316326             0.000000   0.218049   \n",
       "2                           0.000000             0.000000   0.118057   \n",
       "3                           0.156119             0.787603   0.143341   \n",
       "4                           0.146243             0.866498   0.135338   \n",
       "\n",
       "   Diabetes_pedigree_function_tfidf  Age_tfidf  \n",
       "0                          0.003410   0.271919  \n",
       "1                          0.002836   0.250508  \n",
       "2                          0.003357   0.159835  \n",
       "3                          0.000840   0.105602  \n",
       "4                          0.007082   0.102151  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfTransformer\n",
    "tfidf=TfidfTransformer()\n",
    "\n",
    "X_train_tfidf = tfidf.fit_transform(X_train).toarray()\n",
    "\n",
    "feat_names = columns_org + \"_tfidf\"\n",
    "X_train_tfidf = pd.DataFrame(columns = feat_names, data = X_train_tfidf)\n",
    "\n",
    "X_train_tfidf.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据预处理\n",
    "由于数据极度稀疏，数据缩放应采用MinMaxScaler，使得变换后的数据继续保持稀疏。 \n",
    "如果将特征看似词频这种特征，也可以不用缩放，每个样本用模长归一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对原始数据缩放\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "# 构造输入特征的标准化器\n",
    "ms_org = MinMaxScaler()\n",
    "\n",
    "#保存特征名字，用于结果保存为csv\n",
    "feat_names_org = X_train.columns\n",
    "\n",
    "# 用训练训练模型（得到均值和标准差）：fit\n",
    "# 并对训练数据进行特征缩放：transform\n",
    "X_train = ms_org.fit_transform(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对log数据缩放\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "# 构造输入特征的标准化器\n",
    "ms_log = MinMaxScaler()\n",
    "\n",
    "#保存特征名字，用于结果保存为csv\n",
    "feat_names_log = X_train_log.columns\n",
    "\n",
    "# 用训练训练模型（得到均值和标准差）：fit\n",
    "# 并对训练数据进行特征缩放：transform\n",
    "X_train_log = ms_log.fit_transform(X_train_log)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对tf-idf数据缩放\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "#保存特征名字，用于结果保存为csv\n",
    "feat_names_tfidf = X_train_tfidf.columns\n",
    "\n",
    "# 构造输入特征的标准化器\n",
    "ms_tfidf = MinMaxScaler()\n",
    "\n",
    "# 用训练训练模型（得到均值和标准差）：fit\n",
    "# 并对训练数据进行特征缩放：transform\n",
    "X_train_tfidf = ms_tfidf.fit_transform(X_train_tfidf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存原始特征\n",
    "y = pd.Series(data = y_train, name = 'Target')\n",
    "feat_names = columns_org\n",
    "train_org = pd.concat([ pd.DataFrame(columns = feat_names_org, data = X_train), y], axis = 1)\n",
    "train_org.to_csv('Otto_FE_train_org.csv',index=False,header=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存log特征变换结果\n",
    "y = pd.Series(data = y_train, name = 'Target')\n",
    "train_log = pd.concat([ pd.DataFrame(columns = feat_names_log, data = X_train_log), y], axis = 1)\n",
    "train_log.to_csv('Otto_FE_train_log.csv',index=False,header=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存tf-idf特征变换结果\n",
    "y = pd.Series(data = y_train, name = 'Target')\n",
    "train_tfidf = pd.concat([ pd.DataFrame(columns = feat_names_tfidf, data = X_train_tfidf), y], axis = 1)\n",
    "train_tfidf.to_csv('Otto_FE_train_tfidf.csv',index=False,header=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "pickle.dump(tfidf, open(\"tfidf.pkl\", 'wb'))\n",
    "\n",
    "pickle.dump(ms_org, open(\"MinMaxSclaer_org.pkl\", 'wb'))\n",
    "pickle.dump(ms_log, open(\"MinMaxSclaer_log.pkl\", 'wb'))\n",
    "pickle.dump(ms_tfidf, open(\"MinMaxSclaer_tfidf.pkl\", 'wb'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
